//
//  NewsMaintainCell.m
//  CarUser
//
//  Created by 王森林 on 2019/1/18.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "NewsMaintainCell.h"
#import "HomeDetailView.h"
#import "NewsMaintainViewController.h"
#import "HomeArticleDetailViewController.h"
@interface NewsMaintainCell()
@property  (nonatomic, strong) UIView *bgView;
@property  (nonatomic, strong) HomeDetailView *homeDetailView;
@property (nonatomic, strong) NSArray *functionAry;
@end

#define BTNTAG 100000
@implementation NewsMaintainCell

-(void)createUIWithData
{
    [self.contentView removeAllSubviews];
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.homeDetailView];
    [self createConstraint];
    [self.functionAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *imageName  =   [obj  objectForKey:@"imageName"];
        NSString *title = [obj objectForKey:@"title"];
        [self createBtnWithImg:imageName withTitle:title withIndex:idx];
        
    }];
    
    
}

-  (void )createBtnWithImg:(NSString *)imgName withTitle:(NSString *)title withIndex:(NSInteger)index
{
    UIImage *image = IMG(imgName);
    //    float marX = 40;
    //    float  width  = (BOUNDWIDTH  - IMGWIDTH(image)*4-marX*2)/3;
    float width = BOUNDWIDTH/4;
    float  height  = 90;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =  CGRectMake(width*(index%4), 60+ height*(index/4), width, height);
    btn.tag = BTNTAG + index;
    [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [btn setTitle:title forState:UIControlStateNormal];
//    [btn setImage:image forState:UIControlStateNormal];
//    [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
//    btn.titleLabel.font = FONT_SIZE(12);
//    btn.titleLabel.textAlignment  =  NSTextAlignmentCenter;
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
//    //    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
//    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height-20 ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
//    [btn setImageEdgeInsets:UIEdgeInsetsMake( -btn.titleLabel.bounds.size.height-40-8, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((width-50)/2, 0, 50, 50)];
    imageView.image = image;
    [btn addSubview:imageView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, width, 15)];
    titleLabel.text = title;
    titleLabel.font = FONT_SIZE(12);
    titleLabel.textAlignment  =  NSTextAlignmentCenter;
    titleLabel.textColor = Common_TextColor;
    [btn addSubview:titleLabel];
    
    
    
    [self.bgView addSubview:btn];
}

- (void)handleBtnAction:(id)sender
{
//    UIButton *btn = sender;
//    NSInteger tag = btn.tag - BTNTAG;
//    switch (tag) {
//        case 0:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"b87971b9caf14c719b0bdb6c8403f121";
//            controller.title = @"维修保养服务-小保养";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//        case 1:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"456d3c0f3ea8499db7ec494210a76239";
//            controller.title = @"维修保养服务-大保养";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//        case 2:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"315c3797d8684c3c803b6dbbee0c2b64";
//            controller.title = @"维修保养服务-节气门清洗";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//        case 3:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"0ecc37bfa6ae4fa5964c431198befa24";
//            controller.title = @"维修保养服务-更换火花塞";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//
//        }
//            break;
//        case 4:
//        {
////            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
////            controller.article_id = @"0ecc37bfa6ae4fa5964c431198befa24";
////            controller.title = @"维修保养服务-更换火花塞";
////            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//        case 5:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"b6658044e38043d39821c342086afbd9";
//            controller.title = @"维修保养服务-更换刹车油";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//        case 6:
//        {
//
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"605402dfe9b8403aac885b3e002ebf52";
//            controller.title = @"维修保养服务-更换蓄电池";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//        }
//            break;
//        case 7:
//        {
//            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//            controller.article_id = @"4693c37713bb4b4096c3078a1849b2a9";
//            controller.title = @"维修保养服务-发动机舱养护";
//            [[self viewController].navigationController pushViewController:controller animated:YES];
//
//        }
//            break;
//
//        default:
//            break;
//    }
    
    
    NewsMaintainViewController *controller = [[NewsMaintainViewController alloc] init];
    [[self viewController].navigationController pushViewController:controller animated:YES];
    
}

- (void)createConstraint
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(0);
        (void)make.left.and.right;
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.homeDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(0);
        (void)make.left.and.right;
        make.height.mas_equalTo(48);
    }];
    
    
    
}

- (void)setData:(id)data
{
    //    self.imgsAry = @[@"car_test4",@"car_test4",@"car_test4",@"car_test4",@"car_test4"];
     self.functionAry = @[@{@"imageName":@"function1",@"title":@"小保养"},
                     @{@"imageName":@"function2",@"title":@"大保养"},
                     @{@"imageName":@"function3",@"title":@"节气门清洗"},
                     @{@"imageName":@"function4",@"title":@"更换火花塞"},
                            @{@"imageName":@"function5",@"title":@"轮廓保养"},
                            @{@"imageName":@"function6",@"title":@"更换刹车灯"},
                            @{@"imageName":@"function7",@"title":@"更换蓄电池"},
                            @{@"imageName":@"function8",@"title":@"发动机仓保养"}
                     ];
    [self createUIWithData];
}


-  (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
    
}



- (HomeDetailView *)homeDetailView
{
    if (!_homeDetailView) {
        _homeDetailView = [[HomeDetailView alloc] initWithType:HOME_DETAIL_TYPE_MAINTAIN withBlock:^(id  _Nonnull response) {
            NewsMaintainViewController *controller = [[NewsMaintainViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [[self viewController].navigationController  pushViewController:controller animated:YES];
        }];
    }
    return _homeDetailView;
}


@end
